package com.ljb.nfcreadandwritedemo.write;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;

import com.ljb.nfcreadandwritedemo.BaseNFCActivity;
import com.ljb.nfcreadandwritedemo.R;

import java.nio.charset.Charset;

/**
 * Created by ljb on 2018/8/3.
 */
public class WriteMUActivity extends BaseNFCActivity {
    private boolean isWrite = false;
    private EditText etData;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_write);
        etData = findViewById(R.id.et_data);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_write_text:
                isWrite = true;
                break;
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (isWrite) {
            String text = etData.getText().toString().trim();
            if (text.getBytes(Charset.forName("gb2312")).length % 4 != 0) {
                showToast("写入的数据字节数必须为4的倍数");
                return;
            }
            boolean ret = nfcHelper.writeNFC_MU(intent, text);
            nfcHelper.vibrate(this);
            showToast(ret ? "数据写入成功" : "数据写入失败");
            isWrite = false;
        }
    }
}
